package com.gzyj.flink.gps.sink.gp;

import com.gzyj.flink.gps.factory.MybatisSessionFactory;
import com.gzyj.flink.gps.mapper.tidb.GPSMapper;
import com.gzyj.flink.gps.mapper.tidb.GzyjStopHistoryMapper;
import com.gzyj.flink.pojo.gps.VehGps;
import com.gzyj.flink.pojo.gps.VehStopHistory;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StopHistorySink extends RichSinkFunction<VehStopHistory> {

    private static final Logger LOG = LoggerFactory.getLogger(MybatisGPSSink.class);

    private SqlSessionFactory sqlSessionFactory;



    @Override
    public void open(Configuration parameters) throws Exception {
        sqlSessionFactory = MybatisSessionFactory.getSqlSessionFactory();
    }

    @Override
    public void invoke(VehStopHistory value, Context context) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        try {

            GzyjStopHistoryMapper stopHistoryMapper = sqlSession.getMapper(GzyjStopHistoryMapper.class);
            stopHistoryMapper.insertSelective(value);
            sqlSession.commit();
        } catch (Throwable e) {
            sqlSession.rollback();
            LOG.error("MysqlSinkFunction cause Exception,sqlSession transaction rollback...", e);
        }finally {
            try {
                sqlSession.close();
            }catch (Exception e){

            }

        }
    }

}
